package com.sc.justification.system;

import javax.swing.JPanel;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import com.sc.justification.component.ImageDialog;
import com.sc.justification.dao.GenericDao;
import com.sc.justification.model.Institute;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
import javax.swing.Action;
import java.awt.Component;
import javax.swing.border.EmptyBorder;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.Bindings;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import java.util.Date;
import java.awt.event.ActionListener;

public class InstitutePanel extends JPanel {
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	private JTextField textField_7;
	private JTextField textField_8;
	private JTextField textField_9;
	private JTextField textField_10;
	private JTextField textField_11;
	private JTextField textField_12;
	private JTextField textField_13;
	private JTextField textField_14;
	private JTextField textField_15;
	private final Action action = new SwingAction();
	private Institute institute=new Institute();
	private JLabel instituteName;
	private JDialog dialog;
	private GenericDao<Institute> dao=new GenericDao<Institute>(Institute.class);

	/**
	 * Create the panel.
	 */
	public InstitutePanel( final JDialog dialog) {
		institute=dao.getAll().get(0);
		this.dialog=dialog;
		setBorder(new EmptyBorder(10, 10, 10, 10));
		setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JLabel lblNewLabel = new JLabel(Messages.getString("InstitutePanel.lblNewLabel.text")); //$NON-NLS-1$
		add(lblNewLabel, "2, 2, right, default");
		
		instituteName = new JLabel(Messages.getString("InstitutePanel.lblNewLabel_1.text")); //$NON-NLS-1$
		add(instituteName, "4, 2");
		
		JLabel lblNewLabel_2 = new JLabel(Messages.getString("InstitutePanel.lblNewLabel_2.text")); //$NON-NLS-1$
		add(lblNewLabel_2, "8, 2, right, default");
		
		textField = new JTextField();
		add(textField, "10, 2, fill, default");
		textField.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel(Messages.getString("InstitutePanel.lblNewLabel_3.text")); //$NON-NLS-1$
		add(lblNewLabel_3, "2, 4, right, default");
		
		textField_1 = new JTextField();
		add(textField_1, "4, 4, fill, default");
		textField_1.setColumns(10);
		
		JLabel lblNewLabel_4 = new JLabel(Messages.getString("InstitutePanel.lblNewLabel_4.text")); //$NON-NLS-1$
		add(lblNewLabel_4, "8, 4, right, default");
		
		textField_2 = new JTextField();
		add(textField_2, "10, 4, fill, default");
		textField_2.setColumns(10);
		
		JLabel lblNewLabel_5 = new JLabel(Messages.getString("InstitutePanel.lblNewLabel_5.text"));
		add(lblNewLabel_5, "2, 6, right, default");
		
		textField_3 = new JTextField();
		add(textField_3, "4, 6, 7, 1, fill, default");
		textField_3.setColumns(10);
		
		JLabel lblNewLabel_6 = new JLabel(Messages.getString("InstitutePanel.lblNewLabel_6.text")); //$NON-NLS-1$
		add(lblNewLabel_6, "2, 8, right, default");
		
		textField_4 = new JTextField();
		add(textField_4, "4, 8, fill, default");
		textField_4.setColumns(10);
		
		JLabel lblNewLabel_7 = new JLabel(Messages.getString("InstitutePanel.lblNewLabel_7.text")); //$NON-NLS-1$
		add(lblNewLabel_7, "8, 8, right, default");
		
		textField_5 = new JTextField();
		add(textField_5, "10, 8, fill, default");
		textField_5.setColumns(10);
		
		JLabel label = new JLabel(Messages.getString("InstitutePanel.label.text")); //$NON-NLS-1$
		add(label, "2, 10, right, default");
		
		textField_6 = new JTextField();
		add(textField_6, "4, 10, 7, 1, fill, default");
		textField_6.setColumns(10);
		
		JLabel lblNewLabel_8 = new JLabel(Messages.getString("InstitutePanel.lblNewLabel_8.text")); //$NON-NLS-1$
		add(lblNewLabel_8, "2, 12, right, default");
		
		textField_7 = new JTextField();
		add(textField_7, "4, 12, fill, default");
		textField_7.setColumns(10);
		
		JLabel lblNewLabel_9 = new JLabel(Messages.getString("InstitutePanel.lblNewLabel_9.text")); //$NON-NLS-1$
		add(lblNewLabel_9, "8, 12, right, default");
		
		textField_8 = new JTextField();
		add(textField_8, "10, 12, fill, default");
		textField_8.setColumns(10);
		
		JLabel lblNewLabel_10 = new JLabel(Messages.getString("InstitutePanel.lblNewLabel_10.text")); //$NON-NLS-1$
		add(lblNewLabel_10, "2, 14, right, default");
		
		textField_9 = new JTextField();
		add(textField_9, "4, 14, fill, default");
		textField_9.setColumns(10);
		
		JLabel lblNewLabel_11 = new JLabel(Messages.getString("InstitutePanel.lblNewLabel_11.text")); //$NON-NLS-1$
		add(lblNewLabel_11, "8, 14, right, default");
		
		textField_10 = new JTextField();
		add(textField_10, "10, 14, fill, default");
		textField_10.setColumns(10);
		
		JLabel lblNewLabel_12 = new JLabel(Messages.getString("InstitutePanel.lblNewLabel_12.text")); //$NON-NLS-1$
		add(lblNewLabel_12, "2, 16, right, default");
		
		textField_11 = new JTextField();
		add(textField_11, "4, 16, 7, 1, fill, default");
		textField_11.setColumns(10);
		
		JLabel lblNewLabel_13 = new JLabel(Messages.getString("InstitutePanel.lblNewLabel_13.text")); //$NON-NLS-1$
		add(lblNewLabel_13, "2, 18, right, default");
		
		textField_12 = new JTextField();
		add(textField_12, "4, 18, 7, 1, fill, default");
		textField_12.setColumns(10);
		
		JLabel lblNewLabel_14 = new JLabel(Messages.getString("InstitutePanel.lblNewLabel_14.text")); //$NON-NLS-1$
		add(lblNewLabel_14, "2, 20, right, default");
		
		textField_13 = new JTextField();
		add(textField_13, "4, 20, fill, default");
		textField_13.setColumns(10);
		
		JLabel lblNewLabel_15 = new JLabel(Messages.getString("InstitutePanel.lblNewLabel_15.text")); //$NON-NLS-1$
		add(lblNewLabel_15, "8, 20, right, default");
		
		textField_14 = new JTextField();
		add(textField_14, "10, 20, fill, default");
		textField_14.setColumns(10);
		
		JLabel lblNewLabel_16 = new JLabel(Messages.getString("InstitutePanel.lblNewLabel_16.text")); //$NON-NLS-1$
		add(lblNewLabel_16, "2, 22, right, default");
		
		textField_15 = new JTextField();
		add(textField_15, "4, 22, 5, 1, fill, default");
		textField_15.setColumns(10);
		
		JButton btnNewButton = new JButton(Messages.getString("InstitutePanel.btnNewButton.text")); //$NON-NLS-1$
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ImageDialog dialog=new ImageDialog();
				dialog.setVisible(true);
			}
		});
		add(btnNewButton, "10, 22, left, default");
		
		JButton btnNewButton_2 = new JButton(Messages.getString("InstitutePanel.btnNewButton_2.text")); //$NON-NLS-1$
		btnNewButton_2.setAction(action);
		add(btnNewButton_2, "8, 24, right, bottom");
		
		JButton btnNewButton_1 = new JButton(Messages.getString("InstitutePanel.btnNewButton_1.text")); //$NON-NLS-1$
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dialog.setVisible(false);
			}
		});
		add(btnNewButton_1, "10, 24, left, bottom");
		initDataBindings();

	}

	private class SwingAction extends AbstractAction {
		public SwingAction() {
			putValue(NAME, "保存");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
			System.out.println(institute.getLegalRepresentitive());
			dao.update(institute);
		}
	}
	protected void initDataBindings() {
		BeanProperty<Institute, String> instituteBeanProperty = BeanProperty.create("instituteName");
		BeanProperty<JLabel, String> jLabelBeanProperty = BeanProperty.create("text");
		AutoBinding<Institute, String, JLabel, String> autoBinding = Bindings.createAutoBinding(UpdateStrategy.READ_ONCE, institute, instituteBeanProperty, instituteName, jLabelBeanProperty);
		autoBinding.bind();
		//
		BeanProperty<Institute, String> instituteBeanProperty_1 = BeanProperty.create("lisenceNo");
		BeanProperty<JTextField, String> jTextFieldBeanProperty = BeanProperty.create("text");
		AutoBinding<Institute, String, JTextField, String> autoBinding_1 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, institute, instituteBeanProperty_1, textField, jTextFieldBeanProperty);
		autoBinding_1.bind();
		//
		BeanProperty<Institute, String> instituteBeanProperty_2 = BeanProperty.create("legalRepresentitive");
		BeanProperty<JTextField, String> jTextFieldBeanProperty_1 = BeanProperty.create("text");
		AutoBinding<Institute, String, JTextField, String> autoBinding_2 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, institute, instituteBeanProperty_2, textField_1, jTextFieldBeanProperty_1);
		autoBinding_2.bind();
		//
		BeanProperty<Institute, String> instituteBeanProperty_3 = BeanProperty.create("businessScope");
		BeanProperty<JTextField, String> jTextFieldBeanProperty_2 = BeanProperty.create("text");
		AutoBinding<Institute, String, JTextField, String> autoBinding_3 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, institute, instituteBeanProperty_3, textField_3, jTextFieldBeanProperty_2);
		autoBinding_3.bind();
		//
		BeanProperty<Institute, Date> instituteBeanProperty_4 = BeanProperty.create("periodOfValidity");
		BeanProperty<JTextField, String> jTextFieldBeanProperty_3 = BeanProperty.create("text");
		AutoBinding<Institute, Date, JTextField, String> autoBinding_4 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, institute, instituteBeanProperty_4, textField_4, jTextFieldBeanProperty_3);
		autoBinding_4.bind();
		//
		BeanProperty<Institute, String> instituteBeanProperty_5 = BeanProperty.create("authority");
		BeanProperty<JTextField, String> jTextFieldBeanProperty_4 = BeanProperty.create("text");
		AutoBinding<Institute, String, JTextField, String> autoBinding_5 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, institute, instituteBeanProperty_5, textField_5, jTextFieldBeanProperty_4);
		autoBinding_5.bind();
		//
		BeanProperty<Institute, String> instituteBeanProperty_6 = BeanProperty.create("address");
		BeanProperty<JTextField, String> jTextFieldBeanProperty_5 = BeanProperty.create("text");
		AutoBinding<Institute, String, JTextField, String> autoBinding_6 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, institute, instituteBeanProperty_6, textField_6, jTextFieldBeanProperty_5);
		autoBinding_6.bind();
		//
		BeanProperty<Institute, String> instituteBeanProperty_7 = BeanProperty.create("zipcode");
		BeanProperty<JTextField, String> jTextFieldBeanProperty_6 = BeanProperty.create("text");
		AutoBinding<Institute, String, JTextField, String> autoBinding_7 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, institute, instituteBeanProperty_7, textField_7, jTextFieldBeanProperty_6);
		autoBinding_7.bind();
	}
}
